package com.gdth.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gdth.R;
import com.gdth.model.Item;

public class TreeViewCheckAdapter extends BaseExpandableListAdapter{
	
	private Context context;
	private LayoutInflater layoutInflater;
	
	private List<Item> groupList;
    private List<List<Item>> childList;
    
    @SuppressWarnings("unused")
	private int selectedGroupPosition = -1;
    @SuppressWarnings("unused")
	private int selectedChildPosition = -1;
    
    private int layout = R.layout.treeview_item_checked;

    public TreeViewCheckAdapter(Context context, List<Item> groupList, List<List<Item>> childList) {
        this.context = context;
    	this.groupList = groupList;
        this.childList = childList;
    }
    
    public void setSelectedPosition(int selectedGroupPosition, int selectedChildPosition) {
        this.selectedGroupPosition = selectedGroupPosition;
        this.selectedChildPosition = selectedChildPosition;
    }
    
    
    
    /**********Child**********/
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    	Holder holder = null;
        if (convertView == null) {
        	layoutInflater = LayoutInflater.from(this.context);  
        	convertView = layoutInflater.inflate(layout, null);  
        	
        	holder = new Holder();
        	
        	holder.tv = (TextView) convertView.findViewById(R.id.treeview_name);
        	holder.tv.setTag("1");
        	
        	convertView.setTag(holder);
        } else {
        	holder = (Holder) convertView.getTag();
        }

        Item item = (Item) getChild(groupPosition, childPosition);
        
        holder.tv.setText(item.getValue());
        

//        if (groupPosition == selectedGroupPosition) {
//            if (childPosition == selectedChildPosition) {
//            	holder.tv.setBackgroundColor(0xffb6ddee);
//            } else {
//            	holder.tv.setBackgroundColor(Color.TRANSPARENT);
//            }
//        }

        holder.tv.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                setSelectedPosition(groupPosition, childPosition);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    
    
    /**********Group**********/
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    public int getGroupCount() {
        return groupList.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        LinearLayout cotain = new LinearLayout(this.context);
        cotain.setPadding(10, 10, 10, 10);
        cotain.setGravity(Gravity.CENTER_VERTICAL);
        cotain.setBackgroundColor(Color.WHITE);

        ImageView imgIndicator = new ImageView(this.context);
        TextView textView = new TextView(this.context);
        Item item = (Item) getGroup(groupPosition);
        textView.setText(item.getValue());
        textView.setPadding(30, 0, 0, 20);

        if (isExpanded) {
//            imgIndicator.setBackgroundResource(R.drawable.);
        } else {
//            imgIndicator.setBackgroundResource(R.drawable.macro_plus);
        }
        cotain.addView(imgIndicator);
        cotain.addView(textView);
        return cotain;
    }

    
    /**********hasStableIds**********/
    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
	
    public class Holder {  
    	public TextView tv; 
    	
    	void setId(int position) {  
    		tv.setId(position);    
    	}  
    } 
}
